use image::{imageops, DynamicImage, ImageOutputFormat};
use napi::{CallContext, JsBuffer, JsNumber, Result}; // node api rust 版本的实现
use std::fs;

#[napi::js_function(2)]
fn compress_image(ctx: CallContext) -> Result<JsBuffer> {
    let path = ctx
        .get::<napi::JsString>(0)?
        .into_utf8()?
        .as_str()?
        .to_owned();
    let quality = ctx.get::<napi::JsNumber>(1)?.into();

    let img = imageops::resize(
        DynamicImage::open(&path)?,
        800,
        600,
        imageops::FilterType::Nearest,
    );
    let compressed_img = img.to_bytes();

    let js_buffer = ctx.env.create_buffer_with_data(compressed_img)?;

    Ok(js_buffer) //成功
}
